package com.java2007.hotel.service.impl;

import com.java2007.hotel.constant.Constant;
import com.java2007.hotel.dao.AdminDao;
import com.java2007.hotel.dao.impl.AdminDaoImpl;
import com.java2007.hotel.factory.BeanFactory;
import com.java2007.hotel.pojo.Admin;
import com.java2007.hotel.service.AdminService;
import com.java2007.hotel.utils.JdbcUtils;
import com.java2007.hotel.utils.MD5;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.sql.SQLException;


/**
 * @author csl
 * @version V1.0
 * @date 2021/01/21
 **/
public class AdminServiceImpl implements AdminService {
    private AdminDao adminDao = (AdminDao) BeanFactory.getBean(Constant.ADMIN_DAO_IMPL);

    /**
     * 增加一个管理员
     *
     * @param admin
     * @return
     * @throws SQLException
     */
    @Override
    public int addAdmin(Admin admin) throws SQLException {
        admin.setPwd(MD5.md5(admin.getPwd()));
        return adminDao.addAdmin(admin);
    }

    /**
     * 根据名字查找管理员
     *
     * @param name
     * @return
     * @throws SQLException
     */
    @Override
    public Admin loginAdmin(String name, String pwd) throws SQLException {
        Admin admin = adminDao.selectByName(name);
        if (admin != null) {
            if (!admin.getPwd().equals(MD5.md5(pwd))) {
                throw new RuntimeException("用户名或密码错误");
            }
        } else {
            throw new RuntimeException("用户名或密码错误");
        }
        return admin;
    }

}
